package net.hn.hnms.biz.safety.trinity.group.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.api.safety.trinity.model.DangerInfoVo;
import net.hn.hnms.biz.api.safety.trinity.model.HiddenDangerCheckVo;
import net.hn.hnms.biz.api.safety.trinity.model.RiskControlVo;
import net.hn.hnms.biz.api.safety.trinity.model.TreeViolationsManagementVo;
import net.hn.hnms.biz.safety.trinity.company.service.RiskControlCompanyService;
import net.hn.hnms.biz.safety.trinity.entity.*;
import net.hn.hnms.biz.safety.trinity.group.domain.dto.HiddanDangerDto;
import net.hn.hnms.biz.safety.trinity.group.domain.dto.NameAndIntegerDto;
import net.hn.hnms.biz.safety.trinity.group.domain.vo.*;
import net.hn.hnms.biz.safety.trinity.group.service.HiddanDangerGroupService;
import net.hn.hnms.biz.safety.trinity.service.HiddenDangerRecordService;
import net.hn.hnms.biz.safety.trinity.until.Dictionaries;
import net.hn.hnms.sys.common.core.constant.MineBaseConstants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 智呈物联 nzw
 * @date 2023/4/7
 */

@RestController
@RequestMapping("group/manage/hiddenDangerRecord")
@Api(value = "集团端安全隐患控制器", tags = {"集团端安全隐患"})
public class HiddenDangerRecordGroupController {

    private HiddanDangerGroupService hiddanDanGergroupService;
    private HiddenDangerRecordService hiddenDangerRecordService;
    private RiskControlCompanyService riskControlCompanyService;


    @Autowired
    public void setHiddanDanGergroupService(HiddanDangerGroupService hiddanDanGergroupService) {
        this.hiddanDanGergroupService = hiddanDanGergroupService;
    }

    @Autowired
    public void setHiddenDangerRecordService(HiddenDangerRecordService hiddenDangerRecordService) {
        this.hiddenDangerRecordService = hiddenDangerRecordService;
    }

    @Autowired
    public void setRiskControlCompanyService(RiskControlCompanyService riskControlCompanyService) {
        this.riskControlCompanyService = riskControlCompanyService;
    }

    @GetMapping("hiddenDangerPopUpPrompt")
    @ApiOperation("隐患变化弹窗")
    public R<List<HiddenDangerpopUpPromptVO>> hiddenDangerPopUpPrompt(String companyCode) {
        return R.ok(hiddanDanGergroupService.hiddenDangerpopUpPrompt(companyCode == null ? "" : companyCode));
    }

    @GetMapping("hiddenDangerVariety")
    @ApiOperation("隐患变化情况")
    public R<HiddenDangerVarietyVO> hiddenDangerVariety(String companyCode) {
        return R.ok(hiddanDanGergroupService.hiddenDangerVariety(companyCode == null ? "" : companyCode));
    }

    @GetMapping("hiddenDangerMajorState")
    @ApiOperation("重点关注隐患情况")
    public R<List<HiddenDangerMajorStateVO>> hiddenDangerMajorState() {
        return R.ok(hiddanDanGergroupService.hiddenDangerMajorState());
    }

    @GetMapping("riskLevelByMineCode")
    @ApiOperation("按煤企（矿）统计风险数量")
    public R<GroupLevelCount> riskLevelByMineCode(String companyCode) {
        return R.ok(hiddanDanGergroupService.riskLevelByMineCode(companyCode));
    }

    @GetMapping("hiddenDangerCheck")
    @ApiOperation("隐患排查")
    public R<HiddenDangerCheckVo> hiddenDangerCheck(String companyCode) {
        HiddenDangerCheckVo vo = new HiddenDangerCheckVo();
        for (String key : MineBaseConstants.mineMap.keySet()) {
            MineBaseConstants.Mine mine = MineBaseConstants.mineMap.get(key);
            if (StringUtils.isNotBlank(companyCode) && !mine.getCompanyCode().equals(companyCode)) {
                continue;
            }
            HiddenDangerCheckVo v;
            if (Dictionaries.GAOTOUYAOMINECODE.equals(mine.getMineCode())) {
                v = hiddenDangerRecordService.gaotouyaoHiddenDangerCheck(mine.getMineCode());
            } else {
                v = hiddenDangerRecordService.hiddenDangerCheck(mine.getMineCode());
            }
            vo.setDangerCount(vo.getDangerCount() + v.getDangerCount());
            vo.setMajor(vo.getMajor() + v.getMajor());
            vo.setOverdue(vo.getOverdue() + v.getOverdue());
            vo.setRectified(vo.getRectified() + v.getRectified());
        }
        return R.ok(vo);
    }

    @GetMapping("riskControl")
    @ApiOperation("风险管控")
    public R<RiskControlVo> riskControl(String companyCode) {
        return R.ok(riskControlCompanyService.riskControl(companyCode == null ? "" : companyCode));
    }

    @GetMapping("threeViolations")
    @ApiOperation("三违管理")
    public R<TreeViolationsManagementVo> threeViolations(String companyCode) {
        return R.ok(hiddanDanGergroupService.threeViolations(companyCode == null ? "" : companyCode));
    }

    @GetMapping("dangerInformation")
    @ApiOperation("隐患信息（今日）")
    public R<DangerInfoVo> hiddenDanger(String companyCode) {
        DangerInfoVo vo = new DangerInfoVo();
        for (String key : MineBaseConstants.mineMap.keySet()) {
            MineBaseConstants.Mine mine = MineBaseConstants.mineMap.get(key);
            if (StringUtils.isNotBlank(companyCode) && !mine.getCompanyCode().equals(companyCode)) {
                continue;
            }
            DangerInfoVo v;
            if (Dictionaries.GAOTOUYAOMINECODE.equals(mine.getMineCode())) {
                v = hiddenDangerRecordService.gaoTouYaoHiddenDanger(mine.getMineCode());
            } else {
                v = hiddenDangerRecordService.hiddenDanger(mine.getMineCode());
            }
            vo.setAdded(vo.getAdded() + v.getAdded());
            vo.setClosedLoopCount(vo.getClosedLoopCount() + v.getClosedLoopCount());
            vo.setNotClosedLoopCount(vo.getNotClosedLoopCount() + v.getNotClosedLoopCount());
        }
        return R.ok(vo);
    }
    @GetMapping("dangerInformationByDate")
    @ApiOperation("隐患信息（开始和结束时间）")
    public R<DangerInfoVo> dangerInformationByDate(@ApiParam(value = "公司编码") String companyCode,
                                                   @ApiParam(value = "煤矿编码") String mineCode,
                                                   @ApiParam(value = "开始日期，默认不传查询今日时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                   @ApiParam(value = "结束日期，默认不传查询当前时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        DangerInfoVo vo = hiddanDanGergroupService.dangerInformationByDate(startDay, endDay, companyCode, mineCode);

        return R.ok(vo);
    }

    @GetMapping("hiddenDangerSum")
    @ApiOperation("隐患所属单位统计")
    public R<List<MineHiddenCount>> hiddenDangerSum(String companyCode,
                                                    @ApiParam(value = "开始日期，默认不传查询今日时间") @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDay,
                                                    @ApiParam(value = "开始日期，默认不传查询当前时间") @DateTimeFormat(pattern = "yyyy-MM-dd")Date endDay) {
        return R.ok(hiddanDanGergroupService.hiddenDangerSum(companyCode, startDay, endDay));
    }

    @GetMapping("standardLevel")
    @ApiOperation("标准化等级")
    public R<List<MineBaseConstants.Mine>> hiddenDangerSum(String companyCode) {
        List<MineBaseConstants.Mine> list = new ArrayList<>();
        for (String key : MineBaseConstants.mineMap.keySet()) {
            MineBaseConstants.Mine mine = MineBaseConstants.mineMap.get(key);
            if (StringUtils.isNotBlank(companyCode) && !mine.getCompanyCode().equals(companyCode)) {
                continue;
            }
            list.add(mine);
        }
        return R.ok(list);
    }

    /**
     * 隐患列表分页查询
     *
     * @param pageQuery
     * @param hiddanDangerDto
     * @return
     */
    @GetMapping("hiddenDangerList/pageList")
    @ApiOperation("隐患列表分页查询")
    public R<ResponsePage<HiddanDangerVo>> hiddenDangerListPageList(PageQuery pageQuery, HiddanDangerDto hiddanDangerDto) {
        ResponsePage<HiddanDangerVo> page = hiddanDanGergroupService.hiddenDangerListPageList(pageQuery, hiddanDangerDto);
        return R.ok(page);
    }

    @GetMapping("selectStatusSumByhiddenDangerStatus")
    @ApiOperation("根据隐患状态统计")
    public R<HiddenDangerStatusCountVo> selectStatusSumByhiddenDangerStatus(@ApiParam(value = "公司编码") String companyCode,
                                                                            @ApiParam(value = "煤矿编码") String mineCode,
                                                                            @ApiParam(value = "开始日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                                            @ApiParam(value = "结束日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {

        HiddenDangerStatusCountVo hiddenDangerStatusCountVo = hiddanDanGergroupService.selectStatusSumByhiddenDangerStatus(companyCode, mineCode, startDay, endDay);
        return R.ok(hiddenDangerStatusCountVo);
    }

    @GetMapping("selectlevelSumByhiddenDangerLevel")
    @ApiOperation("根据隐患等级统计")
    public R<HiddenDangerLevelCountVo> selectlevelSumByhiddenDangerLevel(@ApiParam(value = "公司编码") String companyCode,
                                                                         @ApiParam(value = "煤矿编码") String mineCode,
                                                                         @ApiParam(value = "开始日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                                         @ApiParam(value = "结束日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        HiddenDangerLevelCountVo hiddenDangerLevelCountVo = hiddanDanGergroupService.selectlevelSumByhiddenDangerLevel(companyCode, mineCode, startDay, endDay);
        return R.ok(hiddenDangerLevelCountVo);
    }


    @GetMapping("selectlevelSumByCompany")
    @ApiOperation("根据公司统计隐患等级数量")
    public R<List<SelectlevelSumByCompanyVo>> selectlevelSumByCompany(@ApiParam(value = "公司编码，默认不传查询所有公司") String companyCode,
                                                                      @ApiParam(value = "开始日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                                      @ApiParam(value = "结束日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        List<SelectlevelSumByCompanyVo> list = hiddanDanGergroupService.selectlevelSumByCompany(companyCode, startDay, endDay);
        return R.ok(list);
    }

    @GetMapping("overdueStatisticsByCompany")
    @ApiOperation("根据公司编码-逾期统计")
    public R<HiddenDangerCountByDayVo> overdueStatisticsByCompany(@ApiParam(value = "公司编码，默认不传查询所有公司") String companyCode,
                                                                  @ApiParam(value = "煤矿编码") String mineCode,
                                                                  @ApiParam(value = "开始日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                                  @ApiParam(value = "结束日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        List<NameAndIntegerDto> list = hiddanDanGergroupService.overdueStatisticsByCompany(companyCode, mineCode, startDay, endDay);
        HiddenDangerCountByDayVo hiddenDangerCountByDayVo = new HiddenDangerCountByDayVo();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        for (NameAndIntegerDto nameAndIntegerDto : list) {
            nameList.add(nameAndIntegerDto.getName());
            valueList.add(nameAndIntegerDto.getValue());
        }
        hiddenDangerCountByDayVo.setName(nameList);
        hiddenDangerCountByDayVo.setValue(valueList);
        return R.ok(hiddenDangerCountByDayVo);
    }
    /**
     * 按天统计隐患数量变化趋势
     */
    @GetMapping("addHiddenDangerCountByDay")
    @ApiOperation("根据公司编码-按天统计隐患数量变化趋势")
    public R<HiddenDangerCountByDayVo> addHiddenDangerCountByDay(@ApiParam(value = "公司编码，默认不传查询所有公司") String companyCode,
                                                                 @ApiParam(value = "煤矿编码") String mineCode,
                                                                 @ApiParam(value = "开始日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                                                                 @ApiParam(value = "结束日期，默认不传查询所有时间") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        HiddenDangerCountByDayVo vo = hiddanDanGergroupService.addHiddenDangerCountByDay(companyCode, mineCode, startDay, endDay);
        return R.ok(vo);

    }

}
